package whf.framework.tools.generator;

import whf.framework.entity.AbstractEntity;
import whf.framework.tools.generator.db.Column;
import whf.framework.tools.generator.db.Table;
import whf.framework.util.BeanUtils;

/**
 * @author wanghaifeng
 *
 */
public class EntityGenerator extends AbstractGenerator {
	private String packageName;

	private String className;

	private String[] props;

	private String[] types;
	
	private Table table;

	public EntityGenerator(String packageName, String className, String[] types, String[] props) {
		this.packageName = packageName;
		this.className = className;
		this.types = types;
		this.props = props;
	}

	public EntityGenerator(String packageName, Table table) {
		this.packageName = packageName;
		this.table = table;
	}

	public String[] getProps(){
		return this.props;
	}
	
	protected void gen() {
		super.setPackageName(this.packageName+".entity");
		super.setSuperClassName(AbstractEntity.class.getName());
		super.setImplementsSerializable(true);
//		super.setInterfaces(new String[]{"I"+this.table.getBoClassName()});
		if(table == null){
			super.setClassName(this.className);
			for (int i = 0; props != null && i < props.length; i++) {
				if(!BeanUtils.hasProperty(AbstractEntity.class, props[i])){
					super.addProp(types[i], props[i],true, true, props[i]);
				}
			}
		} else {
			super.setClassName(table.getBoClassName());
			for(Column col: table.getColumns()){
				String pn = col.getPropertyName();
				if(!BeanUtils.hasProperty(AbstractEntity.class, pn)){
					super.addProp(col.getPropertyType(), col.getPropertyName(), true, true, col.getColumnComment());
//					if(!col.isForeign()){
//						super.addProp(col.getPropertyType(), pn,true, true);
//					}else{
//						String refTableName = col.getReferencedTable();
//						String refBo = Utils.getBoClassName(refTableName, table.getTablePre());
//						String dt = this.packageName+".entity."+refBo;
//						String p = pn;
//						if(p.toLowerCase().endsWith("id")){
//							p = p.substring(0, p.length() - 2);
//						}
//						super.addProp(dt, p, true, true);
//					}
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {

	}
}
